<?php
namespace Components\Account;

class Factory {
  public static function load(array $config) {
    if (!isset($config['adapter']) || empty($config['adapter']) || !is_string($config['adapter'])) {
      throw new \Exception("A account 'adapter' option is required and must be a nonempty string.");
    }

    $className = __NAMESPACE__ . '\\' . ucfirst(strtolower($config['adapter'])) . '\\Adapter';
    if (!class_exists($className)) {
      throw new \Exception($className . ' is non-existent.');
    }

    if (!isset($config['di'])) {
      throw new \Exception('DI is non-existent.');
    }

    unset($config['adapter']);
    return new $className($config);
  }
}
